package com;

import cn.hutool.db.Session;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anjiplus.template.gaea.business.modules.datasettransform.service.IGroovyHandler;
import org.example.quartz.service.OrgService;
import org.example.quartz.service.UtilService;
import org.example.quartz.util.DataSourceUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 建议在idea写好复制整个类到此处，位置report-core/src/test/java/com/DemoGroovyHandler.java
 */
public class DemoGroovyHandler implements IGroovyHandler {
    //orgService
    UtilService utilService = SpringUtil.getBean(UtilService.class);
    //orgService
    OrgService orgService = SpringUtil.getBean(OrgService.class);

    @Override
    public List<JSONObject> transform(List<JSONObject> data, String relationalDataSet) {
        List<JSONObject> newDatas = new ArrayList<>();
        try {
            if (ObjectUtils.isEmpty(data)) {
                System.out.println("CustomerListQuartz-暂无新数据推送");
            } else {
                //获取年度
                JdbcTemplate jdbcTemplate = SpringUtil.getBean(JdbcTemplate.class);
                //数据库连接池
                Session oracleSession = DataSourceUtil.getLSOracleSession();
                //根据公司、账套进行拆分推送，根据组织编码，查询出账套信息，如果有多个账套，轮询进行处理
                List<Map<String, Object>> yyztCodeMap = orgService.selectCusZtCode(jdbcTemplate);
                for (JSONObject jsonObject : data) {
                    String CODE = jsonObject.getString("CODE");
                    String NAME = jsonObject.getString("NAME");
                    System.out.println("CODE:" + CODE);
                    System.out.println("NAME:" + NAME);
                    //客商PK主键(同客户主键、供应商主键)
                    String PK_CUST_SUP = jsonObject.getString("ID");
                    String TS = jsonObject.getString("TS");
                    for (int i = 0; i < yyztCodeMap.size(); i++) {
                        String orgCode = (String) yyztCodeMap.get(i).get("GSCODE");
                        String code = CODE.replace("$", orgCode);
                        JSONObject object = new JSONObject();
                        object.put("YYID", PK_CUST_SUP);
                        object.put("TS", TS);
                        object.put("CODE", code);
                        object.put("NAME", code + NAME + orgCode);
                        object.put("AREACODE", jsonObject.getString("AREACODE"));
                        object.put("AREANAME", jsonObject.getString("AREANAME"));
                        object.put("ORGCODE", orgCode);
                        object.put("DEFAULTCURRENCY", jsonObject.getString("DEFAULTCURRENCY"));
                        object.put("ORGANIZATIONCODE", jsonObject.getString("ORGANIZATIONCODE"));
                        object.put("EXTC_YCODE", jsonObject.getString("EXTC_YCODE"));
                        object.put("EXTC_YNAME", jsonObject.getString("EXTC_YNAME"));
                        object.put("UNITNATURECODE", "");
                        object.put("UNITNATURENAME", "");
                        object.put("EXTC_TYPECODE", "");
                        object.put("EXTC_TYPENAME", "");
                        object.put("ADDRESS", new JSONArray());
                        object.put("CONTACT", new JSONArray());
                        object.put("BANKACCOUNTS", new JSONArray());
                        newDatas.add(object);
                    }
                }
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            //关闭链接
            DataSourceUtil.closeOracleSession();
            DataSourceUtil.closeMysqlSession();
        }
        return newDatas;
    }

    /**
     * 往来单位-查询客户地址信息
     *
     * @param PK_CUST_SUP
     * @return
     */
    public String getAddressSQL(String PK_CUST_SUP) {
        String sql = "--客户收货地址\n" +
                "select\n" +
                "'' AS CODE,\n" +
                "'' AS COUNTRYORREGION,\n" +
                "'' AS ADDRTYPE,\n" +
                "'' AS ISMAIN,\n" +
                "'' AS STREETNO,\n" +
                "'' AS CONTACTNUM,\n" +
                "'' AS REMARK\n" +
                "from\n" +
                "" + utilService.getOracle() + ".bd_custaddress" + utilService.getHZ() + " \n" +
                "where\n" +
                "PK_CUSTOMER = '" + PK_CUST_SUP + "'\n" +
                "UNION\n" +
                "--供应商发货地址\n" +
                "select\n" +
                "'' AS CODE,\n" +
                "'' AS COUNTRYORREGION,\n" +
                "'' AS ADDRTYPE,\n" +
                "'' AS ISMAIN,\n" +
                "'' AS STREETNO,\n" +
                "'' AS CONTACTNUM,\n" +
                "'' AS REMARK \n" +
                "from \n" +
                "" + utilService.getOracle() + ".bd_supaddress" + utilService.getHZ() + " \n" +
                "where \n" +
                "PK_SUPPLIER = '" + PK_CUST_SUP + "'";
        return sql;
    }

    /**
     * 往来单位-查询客户银行信息
     *
     * @param PK_CUST_SUP
     * @return
     */
    public String getBankaccountSQL(String PK_CUST_SUP) {
        String sql = "--客户银行账号\n" +
                "SELECT\n" +
                "'' AS ACCOUNTCODE,\n" +
                "'' AS ACCOUNTNAME,\n" +
                "'' AS INBANK,\n" +
                "'' AS ACCOUNTSTATE,\n" +
                "'' AS PRIVATEORPUBLIC,\n" +
                "'' AS CURRENCYLIST,\n" +
                "'' AS ISMAIN,\n" +
                "'' AS COUNTRY,\n" +
                "'' AS PROVINCE,\n" +
                "'' AS CITY,\n" +
                "'' AS REMARK\n" +
                "FROM\n" + " " + utilService.getOracle() + ".BD_CUSTBANK" + utilService.getHZ() + "\n" +
                "WHERE \n" +
                "PK_CUST = '" + PK_CUST_SUP + "'";
        return sql;
    }

    /**
     * 往来单位-查询客户联系人信息
     *
     * @param PK_CUST_SUP
     * @return
     */
    public String getContactSQL(String PK_CUST_SUP) {
        String hz1 = " " + utilService.getOracle() + ".BD_CUSTLINKMAN\n";
        String hz2 = " " + utilService.getOracle() + ".BD_SUPLINKMAN\n";
        if (!ObjectUtils.isEmpty(utilService.getOracleback())) {
            hz1 = " " + utilService.getOracle() + ".BD_CUSTLINKMAN" + utilService.getHZ() + "\n";
            hz2 = " " + utilService.getOracle() + ".BD_SUPLINKMAN" + utilService.getHZ() + "\n";
        }
        String sql = "--客户联系人\n" +
                "SELECT\n" +
                "'' AS NAME,\n" +
                "'' AS IDENTITYTYPE,\n" +
                "'' AS IDENTITYNUMBER,\n" +
                "'' AS GENDER,\n" +
                "'' AS DUTY,\n" +
                "'' AS TEL,\n" +
                "'' AS ISMAIN,\n" +
                "'' AS REMARK \n" +
                "FROM\n" +
                hz1 +
                "WHERE \n" +
                "PK_CUSTOMER = '" + PK_CUST_SUP + "'\n" +
                "UNION\n" +
                "--供应商联系人\n" +
                "SELECT\n" +
                "'' AS NAME,\n" +
                "'' AS IDENTITYTYPE,\n" +
                "'' AS IDENTITYNUMBER,\n" +
                "'' AS GENDER,\n" +
                "'' AS DUTY,\n" +
                "'' AS TEL,\n" +
                "'' AS ISMAIN,\n" +
                "'' AS REMARK  \n" +
                "FROM\n" +
                hz2 +
                "WHERE \n" +
                "PK_SUPPLIER = '" + PK_CUST_SUP + "'";
        return sql;
    }
}